﻿using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util;
using Imaginecup.factory;
using Microsoft.Xna.Framework;
using imaginecup.util;
using Imaginecup.sprite;
using Imaginecup.util.collision;
using Imaginecup.sprite.animation;

namespace Imaginecup.gameState.temp
{
    public class Title : GameState
    {
        enum State { Delay, Animation };

        private ContentManager m_Content;

        private static Title m_Title = null;

        private EntityInfo m_BackInfo;
        private ISprite m_BackSprite;

        private EntityInfo m_TitleNameInfo;
        private ISprite m_TitleNameSprite;

        private State m_State;

        private double m_Delay = 1.5;
        private double m_CurrentTime;
        private int m_AnimationPrevCount;

        private AnimationIndexMan m_AnimationIndex;


        private ISprite m_WordSprite;
        private EntityInfo m_WorldInfo;

        private bool m_bOnWord = true;
        private double m_WordDelay = 0.5;
        private double m_WordCurrentTime = 0;

        private Title()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";

        }

        public static Title GetTitle()
        {
            if (m_Title == null)
            {
                m_Title = new Title();
            }
            return m_Title;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_BackInfo = new EntityInfo(0, 0, 800, 480);
            m_TitleNameInfo = new EntityInfo(0, 55, 447, 289);

            m_AnimationIndex = new AnimationIndexMan();
            m_AnimationIndex.SetDelay(0.125);
            m_AnimationIndex.SetIndex(Vector2.Zero, 6);
            m_AnimationIndex.StartAnimation();

            m_State = State.Delay;
            m_CurrentTime = 0;

            m_WordCurrentTime = 0;
            m_AnimationPrevCount = 0;
            m_bOnWord = true;

            m_WorldInfo = new EntityInfo(250, 400, 257, 34);

        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            ResourceMan resourceMan = ResourceMan.GetResourceMan();

            AnimationSpriteByTexture sprite = new AnimationSpriteByTexture(m_AnimationIndex);
            sprite.AddTextureName("title\\Title_Name1");
            sprite.AddTextureName("title\\Title_Name2");
            sprite.AddTextureName("title\\Title_Name3");
            sprite.AddTextureName("title\\Title_Name4");
            sprite.AddTextureName("title\\Title_Name5");
            sprite.AddTextureName("title\\Title_Name6");

            m_TitleNameSprite = sprite;


            NormalSprite backSprite = new NormalSprite();
            backSprite.SetTextureName("Back");
            m_BackSprite = backSprite;
            
            NormalSprite wordSprite = new NormalSprite();
            wordSprite.SetTextureName("Word");
            m_WordSprite = wordSprite;


            ResourceMan.GetResourceMan().SetTextureFrame("Back", m_Content.Load<Texture2D>("title\\Back"));
            ResourceMan.GetResourceMan().SetTextureFrame("Word", m_Content.Load<Texture2D>("title\\title_touch"));
                                  

            SoundMan.GetSoundMan().LoadSong("TitleBGM", "sound\\Title");
            SoundMan.GetSoundMan().PlayBackSound("TitleBGM");
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_BackSprite.Release();
            ResourceMan.GetResourceMan().Release();
            m_Content.Unload();

        }



        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();

            if (input.IsOneClickUp())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
                return;
            }
            if (m_State == State.Animation)
            {
                m_AnimationIndex.Update(gameTime);

                if (m_AnimationIndex.GetCycleCount() != m_AnimationPrevCount)
                {
                    m_State = State.Delay;
                    m_AnimationPrevCount = m_AnimationIndex.GetCycleCount();
                }
            }
            else
            {
                m_CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;
                if (m_CurrentTime > m_Delay)
                {
                    m_CurrentTime = 0;
                    m_State = State.Animation;
                }
            }

            m_WordCurrentTime += gameTime.ElapsedGameTime.TotalSeconds;
            if( m_WordCurrentTime > m_WordDelay )
            {
                m_WordCurrentTime -= m_WordDelay;
                m_bOnWord = !m_bOnWord;
            }
        }


        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            m_BackSprite.Draw(sb, m_BackInfo);
            m_TitleNameSprite.Draw(sb, m_TitleNameInfo);
           
            if( m_bOnWord)
                 m_WordSprite.Draw(sb, m_WorldInfo);
            base.Draw(gameTime);
        }
    }
}
